package service.client.managers;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;

import client.entities.ClientVO;
import client.exceptions.BadClientException;
import client.exceptions.ClientNotHereException;
import client.exceptions.IncompleteClientException;
import client.exceptions.MissingClientException;
import client.interfaces.ClientRemoteMgt;
import dbExceptions.DataBaseErrorException;
import service.Factory;
import service.article.exceptions.ProxyException;
import service.client.interfaces.ClientProxyMgt;

public class ClientProxyMgr implements ClientProxyMgt {

	private static ClientProxyMgr instance = null;

	private ClientProxyMgr() {
	}

	public static ClientProxyMgr getInstance() {
		if (instance == null)
			instance = new ClientProxyMgr();
		return instance;
	}

	public void addClient(ClientVO client) throws RemoteException,
			ProxyException, IncompleteClientException, MissingClientException,
			DataBaseErrorException, BadClientException {
		ClientRemoteMgt temp = this.getClientRemoteMgt();
		temp.addClient(client);
	}

	public ClientVO[] getClients() throws RemoteException, ProxyException {
		ClientRemoteMgt temp = this.getClientRemoteMgt();
		return temp.getClients();
	}

	public void updateClient(ClientVO client) throws RemoteException,
			ProxyException, IncompleteClientException, MissingClientException,
			BadClientException, DataBaseErrorException {
		ClientRemoteMgt temp = this.getClientRemoteMgt();
		temp.updateClient(client);
	}

	public void deleteClient(ClientVO client) throws RemoteException,
			ProxyException, MissingClientException, DataBaseErrorException,
			BadClientException {
		ClientRemoteMgt temp = this.getClientRemoteMgt();
		temp.deleteClient(client);

	}

	public ClientVO getClient(int ci) throws RemoteException, ProxyException,
			ClientNotHereException {
		ClientRemoteMgt temp = this.getClientRemoteMgt();
		return temp.getClient(ci);
	}

	public ClientRemoteMgt getClientRemoteMgt() throws ProxyException {
		ClientRemoteMgt temp = null;
		String sName = Factory.getInstance().getClient();
		Registry oRegitry;
		try {
			oRegitry = Factory.getInstance().getRegistry();
			temp = (ClientRemoteMgt) oRegitry.lookup(sName);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		if (temp == null) {
			throw new ProxyException("Error en el RMI");
		}
		return temp;
	}

}
